<template>
  <el-dialog :title="title" :visible.sync="open" width="630px" append-to-body>
    <el-transfer
      :titles="['用户', '需要授权人员']"
      v-loading="loading"
      filterable
      v-model="value"
      :props="{key: 'id',label: 'cnName'}"
      :data="data"
      @change="dataChange"
    ></el-transfer>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>    
</template>

<script>
import { listUserAll } from "@/api/system/user";
export default {
  name: "selectUserComp",
  props: {
    title: {
      type: String,
      default: "选择人员",
    },
  },
  data() {
    return {
      open: false,
      loading: false,
      data: [],
      value: []
    }
  },
  computed: {
    transferData() {
      let template = this.data.map((item, index) => {
        return {
          key: item.id,
          label: item.cnName,
        }
      })
      return template;
    }
  },
  methods: {
    dataChange(value, direction, movedKeys) {
      console.log(value, direction, movedKeys)
    },
    show(ids) {
      this.open = true;
      this.value = ids && ids.length ? ids : [];
      this.getList();
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUserAll().then(response => {
        this.data = response.data;
      }).finally(() => {
        this.loading = false;
      })
    },
    submit() {
      if (!this.value || !this.value.length) {
        this.$message.error("请选择需要授权的用户");
        return ;
      }
      let selectedObj = this.data.filter(item => this.value.includes(item.id))
      this.$emit("selected", this.value, selectedObj)
      this.open = false;
    },
    cancel() {
      this.open = false;
    }
  }
}
</script>

<style>

</style>